package chixing.day17.work;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class EmployeeDemo {
    static List<Employee> employeeList = new ArrayList<>();
    public static void main(String[] args) {
        readFile("D:\\a\\salaries.txt");
        employeeList.forEach(System.out::println);
        writeFile("D:\\a\\high_salary_report.txt");
    }
    public static void writeFile(String path) {
        BufferedWriter bufferedWriter = null;
        try {
            bufferedWriter = new BufferedWriter(new FileWriter(path));
            for (Employee employee : employeeList) {
                if (employee.getEmpSalary() > 20000) {
                    bufferedWriter.write(employee.getEmpId()  + " " +employee.getEmpName() + " " + employee.getEmpSalary());
                    bufferedWriter.newLine();
                    bufferedWriter.flush();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferedWriter != null) {
                try {
                    bufferedWriter.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    public static void readFile( String path) {
        BufferedReader bufferedReader = null;
        try {
            bufferedReader = new BufferedReader(new FileReader(path));
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }
        String employeeData=null;
        try {
        String title =bufferedReader.readLine();
        while((employeeData= bufferedReader.readLine())!=null) {
            Employee employee = parseToEmployee(employeeData);
            employeeList.add(employee);
        }
        }catch (IOException e) {
            e.printStackTrace();
        }
    }
    public static Employee parseToEmployee(String line) {
        Employee employee = null;
        String[] split = line.split("\\|");
        Integer empId = Integer.valueOf(split[0]);
        String empName = split[1];
        double empSalary = Double.valueOf(split[2]);
         employee = new Employee(empId, empName, empSalary);
        return employee;
    }
}
